package com.ayu.scalalearn.chapter07

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer

/**
  * @Author 徐林玉
  * @Description Test02ArrayBuffer
  * @Date 2022/8/3 9:21
  * @Version 1.0
  */
object Test02ArrayBuffer {
  def main(args: Array[String]): Unit = {


    //1、创建可变数组
    var arr1: ArrayBuffer[Int] = new ArrayBuffer[Int]()
    var arr2: ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)
    println(arr1)
    println(arr2)

    //2、访问元素
    println(arr2(1))

    arr2(1) = 55

    println(arr2(1))


    println("=======================")
    println()


    //3、添加元素
    arr2 += 4
    println(arr2)
    0 +=: arr2
    println(arr2)

    println("============")

    arr1.append(2)

    arr1.prepend(1)

    //insert第一个就是位置
    arr1.insert(2, 5, 6)

    println("arr1=======>" + arr1)


    println("================")


    arr2.insertAll(arr2.length - 1, arr1)

    println("arr2:insertAll:" + arr2)

    arr2.prependAll(arr1)

    println("arr2:prependAll:" + arr2)


    //可变数组变成不可变数组
    val arr3: ArrayBuffer[Int] = ArrayBuffer(11, 12, 13)

    val newArr: Array[Int] = arr3.toArray

    println(newArr.mkString(","))

    println(arr3)


    println("=========================")

    //不可变数组变成可变
    val buffer: mutable.Buffer[Int] = newArr.toBuffer
    println(buffer)
    println(newArr.mkString(","))
    println()


  }

}
